package com.hs.biye.controller.admin;

import com.hs.biye.constant.MessageConstant;
import com.hs.biye.excepction.BaseException;
import com.hs.biye.model.DTO.MedicineRequest;
import com.hs.biye.model.DTO.PutMedicineRequest;
import com.hs.biye.model.VO.MedicineVo;
import com.hs.biye.model.VO.MedinineType;
import com.hs.biye.model.entity.Description;
import com.hs.biye.model.reslut.Result;
import com.hs.biye.model.reslut.ResultPage;
import com.hs.biye.service.MedicineService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/admin/med")
public class MedicineController {

    @Resource
    private MedicineService medicineService;

    //查询药品信息
    @PostMapping("/pages")
    public ResultPage<MedicineVo> getMedicinePage(@RequestBody MedicineRequest medicineRequest){
        if (medicineRequest == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        return medicineService.getPageMedicine(medicineRequest);
    }


    //查询药品信息
    @GetMapping("/one/{id}")
    public Result<MedicineVo> getMedicine(@PathVariable Integer id){
        if (id == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        MedicineVo medicineVo = medicineService.getMedicineOneById(id);
        if (medicineVo == null){
            throw new BaseException(MessageConstant.NOT_QUERY);
        }
        return Result.success(medicineVo);
    }

    @GetMapping("/unit")
    public Result<List<HashMap<String, String>>> getMedicineType(){
        List<String> list = Arrays.asList("盒", "瓶", "袋");
        List<HashMap<String, String>> res = new ArrayList<>();
        list.forEach(i->{
            HashMap<String, String> map = new HashMap<>(1);
            map.put("unit",i);
            res.add(map);
        });

        return Result.success(res);
    }

    @GetMapping("/type")
    public Result<List<MedinineType>> getMedicineTypes(){
        String[] mdeicines = MessageConstant.mdeicines;
        List<String> list = Arrays.asList(mdeicines);
        List<MedinineType> res = new ArrayList<>();
        list.forEach(i->{
            MedinineType medinineType = new MedinineType();
            medinineType.setMedicineType(i);
            res.add(medinineType);
        });
        return Result.success(res);
    }
    



    //修改药品
    @PostMapping("/update")
    public Result<Boolean> updateMedicine(@RequestBody MedicineRequest medicineRequest){
        if (medicineRequest == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        boolean result = medicineService.updateMedicine(medicineRequest);

        return Result.success(result);
    }
    //添加药品
    @PostMapping("/add")
    public Result<Boolean> addMedicine(@RequestBody MedicineRequest medicineRequest){
        if (medicineRequest == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        boolean result = medicineService.addMedicine(medicineRequest);
        return Result.success(result);
    }

    //删除药品
    @GetMapping("/delete/{id}")
    public Result<Boolean> deleteMedicine(@PathVariable Integer id){
        if (id == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        boolean result = medicineService.removeMedicine(id);
        return Result.success(result);
    }

    //配药接口
    @PostMapping("/putmedicine")
    public Result<Boolean> putmedicine(@RequestBody PutMedicineRequest putMedicineRequest){
        if (putMedicineRequest == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        boolean result = medicineService.putMedicine(putMedicineRequest);
        return Result.success(result);
    }
}
